package factorytest.iflytek.com.einkiflytekfactorytest.engineerui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import androidx.core.app.ActivityCompat;
import factorytest.iflytek.com.einkiflytekfactorytest.R;


public class PenCalibActivity extends Activity implements PenCalibTextView.OnClickAllCheckPointCallback {
	public static String TAG = "CalibActivity";

	// the broadcast that calibration done/update,Don't change!!
	public static String CALIB_ACTION = "action.pen_calibration";
	public static String CALIB_EXTER_X = "CX";
	public static String CALIB_EXTER_Y = "CY";

	private Context mContext;

	private PenCalibTextView	mPenCalibTextView;
	private Button				mCalibDoneButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        setFullScreen();

        setContentView(R.layout.pencalib_activity);

		mContext = this;

		mPenCalibTextView = (PenCalibTextView) findViewById(R.id.pencalib_textView);
//        mPenCalibTextView.setAppEinkMode(View.EINK_DATA_MODE_DEFAULT, View.EINK_SHOW_MODE_GC);
        //mPenCalibTextView.forceEinkFullUpdate();

		mPenCalibTextView.setCheckPointCallback(this);

        mCalibDoneButton = (Button) findViewById(R.id.pencalib_btn_done);
        mCalibDoneButton.setEnabled(false);

        mCalibDoneButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(RESULT_OK);
                ActivityCompat.finishAfterTransition(PenCalibActivity.this);
            }
        });

    }

    private void setFullScreen() {
    	requestWindowFeature(Window.FEATURE_NO_TITLE);
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION  // hide naviagtion,and whow again when touch
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;  //20180318, fix the UI for dialog.
        decorView.setSystemUiVisibility(uiOptions);
    }

    public static void sendCalibBroadcast(Context ctx, int cx, int cy) {
    	Intent intent = new Intent(CALIB_ACTION);
    	intent.putExtra(CALIB_EXTER_X, cx);
    	intent.putExtra(CALIB_EXTER_Y, cy);
    	ctx.sendBroadcast(intent);
    }

	@Override
	public void onClickAllCheckPointCallback(int calibX, int calibY) {
		sendCalibBroadcast(mContext, calibX, calibY);
        mCalibDoneButton.setEnabled(true);
	}
}